/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ec.edu.espe.ccsoft.mantenimiento.bean.converters;

import ec.edu.espe.ccsoft.mantenimiento.dao.MecanicoFacade;
import ec.edu.espe.ccsoft.mantenimiento.dao.TallerFacade;
import ec.edu.espe.ccsoft.mantenimiento.modelos.Mecanico;
import ec.edu.espe.ccsoft.mantenimiento.modelos.Taller;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author Carlos
 */
@FacesConverter(value = "mecanicoConverter", forClass = ec.edu.espe.ccsoft.mantenimiento.modelos.Mecanico.class)
public class MecanicoConverter implements Converter {
    
    @EJB
    private MecanicoFacade mecanicoFacade;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
        int mecanicoId;

        try {
            mecanicoId = Integer.parseInt(submittedValue);
        } catch (NumberFormatException exception) {
            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    "Type the name of a Course and select it (or use the dropdown)",
                    "Type the name of a Course and select it (or use the dropdown)"));
        }

        return mecanicoFacade.find(mecanicoId);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object modelValue) {
        Mecanico mecanico = null;
        if (modelValue instanceof Mecanico) {
            mecanico = (Mecanico) modelValue;
            StringBuilder asString = new StringBuilder();
            asString.append(mecanico.getCodMecanico());
            return asString.toString();
        }
        return "";
    }

}
